package org.shj;

import cn.hutool.core.io.resource.UrlResource;
import lombok.Data;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Shen Huang Jian
 * @date 2020-08-06 14:19
 */
public class Test {

    public static void main(String[] args) throws Exception{
        String tableName = "scott.Asdt";
        String[] tableArr = tableName.split("\\.");

        Long a = new Long(100);
        Long b = 100L;

        System.out.println(a.longValue() == b);

        SourceObj sourceObj = new SourceObj();
        sourceObj.setUsername("aaaa");
        sourceObj.setSpeed(22L);

        TargetObj targetObj = new TargetObj();
//        Map<String, String> map = new HashMap<>();
//        map.put("username", "name");
//        map.put("speed", "speed");
//        convert(sourceObj, targetObj, map);
//        System.out.println(targetObj.getName());
        targetObj.setSpeed(sourceObj.getSpeed().doubleValue());

        String path = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1606306823973&di=872e37b9e434d3d552c1fa0d0a7837ec&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F55%2F22%2F20300000929429130630222900050.jpg";

        URL url = new URL(path);
//        UrlResource urlResource = new UrlResource(url);

        // create an image input stream from the specified fileDD
        ImageInputStream iis = ImageIO.createImageInputStream(url.openStream());

        // get all currently registered readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

        if (!iter.hasNext()) {
            throw new RuntimeException("No readers found!");
        }

        // get the first reader
        ImageReader reader = iter.next();

        System.out.println("Format: " + reader.getFormatName());

        // close stream
        iis.close();

    }

    public static <S,T> void convert(S source, T target, Map<String, String> sourceTargetFieldMap){
        Class<?> sClass = source.getClass();
        Class<?> tClass = target.getClass();

        for(String sourceFieldName : sourceTargetFieldMap.keySet()){
            String targetFieldName = sourceTargetFieldMap.get(sourceFieldName);
            try {
                Field sourceField = sClass.getDeclaredField(sourceFieldName);
                sourceField.setAccessible(true);
                Object sourceFieldValue = sourceField.get(source);
                Field targetField = tClass.getDeclaredField(targetFieldName);
                targetField.setAccessible(true);
                targetField.set(target, sourceFieldValue);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    @Data
    static class SourceObj{
        private String username;

        private Long speed;
    }

    @Data
    static class TargetObj{
        private String name;
        private Double speed;
    }
}
